home *** CD-ROM | disk | FTP | other *** search
/ Amiga Developer CD 2.1 / Amiga Developer CD v2.1.iso / NDK / NDK_3.1 / Examples1 / intuition / pointerdemo.c < prev    next >
Encoding:
C/C++ Source or Header  |  1999-10-27  |  9.6 KB  |  391 lines

  1. /*
  2.  * pointerdemo.c - shows off new pointer features
  3.  *
  4.  * (c) Copyright 1992-1999 Amiga, Inc.  All rights reserved.
  5.  *
  6.  * This software is provided as-is and is subject to change; no warranties
  7.  * are made.  All use is at your own risk.  No liability or responsibility
  8.  * is assumed.
  9.  *
  10.  * This example shows off some of the V39 pointer features available
  11.  * through the boopsi pointerclass and through the new SetWindowPointer()
  12.  * call.  These features include AA-specific pointer features, as well
  13.  * as the standard busy pointer and the pointer-delay feature.  This
  14.  * feature is useful if the application may be busy for an indeterminate
  15.  * but possibly short period of time.  If the application restores the
  16.  * original pointer before a short time has elapsed, no busy pointer
  17.  * will appear.  This removes annoying short flashes of the busy pointer.
  18.  */
  19.  
  20. /*------------------------------------------------------------------------*/
  21.  
  22. #include <intuition/intuitionbase.h>
  23. #include <intuition/pointerclass.h>
  24. #include <intuition/gadgetclass.h>
  25. #include <libraries/gadtools.h>
  26. #include <graphics/gfxbase.h>
  27.  
  28. #include <clib/intuition_protos.h>
  29. #include <clib/gadtools_protos.h>
  30. #include <clib/graphics_protos.h>
  31. #include <clib/exec_protos.h>
  32.  
  33. #include <stdlib.h>
  34. #include <ctype.h>
  35.  
  36. /*------------------------------------------------------------------------*/
  37.  
  38. void doPointerStuff( struct Window *win, ULONG code );
  39. void bail_out(long code);
  40. struct Gadget *createAllGadgets( struct Gadget **glistptr, void *vi,
  41.     struct Screen *sc );
  42. BOOL createPointers(void);
  43.  
  44. /*------------------------------------------------------------------------*/
  45.  
  46. struct NewWindow newwin =
  47. {
  48.     0,0,        /*  LeftEdge, TopEdge */
  49.     320,200,            /*  Width, Height */
  50.     (UBYTE)-1, (UBYTE)-1,             /*  DetailPen, BlockPen */
  51.     IDCMP_VANILLAKEY | IDCMP_CLOSEWINDOW |
  52.     IDCMP_REFRESHWINDOW | BUTTONIDCMP,    /*  IDCMPFlags */
  53.     WFLG_DRAGBAR | WFLG_SIZEGADGET | WFLG_DEPTHGADGET | WFLG_CLOSEGADGET |
  54.     WFLG_ACTIVATE|WFLG_SIMPLE_REFRESH,    /* Flags */
  55.     NULL,        /*  FirstGadget */
  56.     NULL,        /*  CheckMark */
  57.     (UBYTE *) "New Pointer Demo",    /*  Title */
  58.     NULL,        /*  Screen */
  59.     NULL,        /*  BitMap */
  60.     100,50,        /*  MinWidth, MinHeight */
  61.     640,200,        /*  MaxWidth, MaxHeight */
  62.     WBENCHSCREEN,    /*  Type */
  63. };
  64.  
  65. /*------------------------------------------------------------------------*/
  66.  
  67. struct GfxBase *GfxBase = NULL;
  68. struct IntuitionBase *IntuitionBase = NULL;
  69. struct Library *GadToolsBase = NULL;
  70.  
  71. struct Screen *mysc = NULL;
  72. struct VisualInfo *vi = NULL;
  73. struct Gadget *glist = NULL;
  74.  
  75. struct Window *win = NULL;
  76. struct BitMap *bm = NULL;
  77. struct BitMap simplebm;
  78.  
  79. VOID *simple_pointer = NULL;
  80. VOID *wide_pointer = NULL;
  81.  
  82. UWORD __chip simplePointer0[] =
  83. {
  84.     0x0180,    /* .......##....... */
  85.     0x0180,    /* .......##....... */
  86.     0x0180,    /* .......##....... */
  87.     0x0ff0,    /* ....########.... */
  88.     0x3ffc,    /* ..############.. */
  89.     0x7ffe,    /* .##############. */
  90.     0xffff,    /* ################ */
  91.     0xffff,    /* ################ */
  92.     0xffff,    /* ################ */
  93.     0xffff,    /* ################ */
  94.     0xffff,    /* ################ */
  95.     0xffff,    /* ################ */
  96.     0x7ffe,    /* .##############. */
  97.     0x7ffe,    /* .##############. */
  98.     0x3ffc,    /* ..############.. */
  99.     0x0ff0     /* ....########.... */
  100. };
  101.  
  102. UWORD __chip simplePointer1[] =
  103. {
  104.     0x0000,    /* ................ */
  105.     0x0000,    /* ................ */
  106.     0x0000,    /* ................ */
  107.     0x0000,    /* ................ */
  108.     0x0000,    /* ................ */
  109.     0x0660,    /* .....##..##..... */
  110.     0x0440,    /* .....#...#...... */
  111.     0x0000,    /* ................ */
  112.     0x0000,    /* ................ */
  113.     0x0000,    /* ................ */
  114.     0x0000,    /* ................ */
  115.     0x0ef0,    /* ....###.####.... */
  116.     0x07e0,    /* .....######..... */
  117.     0x0340,    /* ......##.#...... */
  118.     0x0000,    /* ................ */
  119.     0x0000     /* ................ */
  120. };
  121.  
  122. struct TextAttr topaz80 =
  123. {
  124.     "topaz.font",
  125.     8,
  126.     FS_NORMAL,
  127.     FPF_ROMFONT|FPF_DESIGNED,
  128. };
  129.  
  130. /*------------------------------------------------------------------------*/
  131.  
  132. void main( void )
  133. {
  134.     BOOL terminated;
  135.     struct IntuiMessage *imsg;
  136.     struct Gadget *gad;
  137.  
  138.     terminated = FALSE;
  139.     win = NULL;
  140.  
  141.     if (!(GfxBase = (struct GfxBase *)
  142.     OpenLibrary("graphics.library",39L)))
  143.     bail_out(20);
  144.  
  145.     if (!(IntuitionBase = (struct IntuitionBase *)
  146.     OpenLibrary("intuition.library",39L)))
  147.     bail_out(20);
  148.  
  149.     if (!(GadToolsBase = OpenLibrary("gadtools.library",39L)))
  150.     bail_out(20);
  151.  
  152.     if ( !( mysc = LockPubScreen( NULL ) ) )
  153.     bail_out(20);
  154.  
  155.     if ( !( vi = GetVisualInfo( mysc, NULL ) ) )
  156.     bail_out(20);
  157.  
  158.     if ( ! createPointers() )
  159.     bail_out(20);
  160.  
  161.     if ( !( gad = createAllGadgets( &glist, vi, mysc ) ) )
  162.     bail_out(20);
  163.  
  164.     if (!(win = OpenWindowTags(&newwin,
  165.     WA_Pointer, simple_pointer,
  166.     WA_PubScreen, mysc,
  167.     WA_Gadgets, glist,
  168.     TAG_DONE)))
  169.     bail_out(20);
  170.  
  171.     while (!terminated)
  172.     {
  173.     Wait (1 << win->UserPort->mp_SigBit);
  174.     while (imsg = GT_GetIMsg(win->UserPort))
  175.     {
  176.         if (imsg->Class == IDCMP_CLOSEWINDOW)
  177.         terminated = TRUE;
  178.         else if (imsg->Class == IDCMP_REFRESHWINDOW)
  179.         {
  180.         GT_BeginRefresh(win);
  181.         GT_EndRefresh(win,TRUE);
  182.         }
  183.         else if (imsg->Class == IDCMP_GADGETUP)
  184.         {
  185.         doPointerStuff( win, ((struct Gadget *)imsg->IAddress)->GadgetID );
  186.         }
  187.         else if (imsg->Class == IDCMP_VANILLAKEY)
  188.         {
  189.         doPointerStuff( win, imsg->Code );
  190.         }
  191.         GT_ReplyIMsg(imsg);
  192.     }
  193.     }
  194.     bail_out(0);
  195. }
  196.  
  197.  
  198. /*------------------------------------------------------------------------*/
  199.  
  200. void doPointerStuff( struct Window *win, ULONG code )
  201. {
  202.     switch ( toupper(code) )
  203.     {
  204.     case 'C':    /* Clear pointer */
  205.     SetWindowPointer( win,
  206.         TAG_DONE );
  207.     break;
  208.  
  209.     case 'S':    /* Set custom */
  210.     SetWindowPointer( win,
  211.         WA_Pointer, simple_pointer,
  212.         TAG_DONE );
  213.     break;
  214.  
  215.     case 'W':    /* Set wide custom */
  216.     SetWindowPointer( win,
  217.         WA_Pointer, wide_pointer,
  218.         TAG_DONE );
  219.     break;
  220.  
  221.     case 'B':    /* Set Busy */
  222.     SetWindowPointer( win,
  223.         WA_BusyPointer, TRUE,
  224.         TAG_DONE );
  225.     break;
  226.  
  227.     case 'D':    /* Busy w/ delay */
  228.     SetWindowPointer( win,
  229.         WA_BusyPointer, TRUE,
  230.         WA_PointerDelay, TRUE,
  231.         TAG_DONE );
  232.     break;
  233.     }
  234. }
  235.  
  236.  
  237. /*------------------------------------------------------------------------*/
  238.  
  239. struct Gadget *createAllGadgets( struct Gadget **glistptr, void *vi,
  240.     struct Screen *sc )
  241. {
  242.     struct NewGadget ng;
  243.     struct Gadget *gad;
  244.  
  245.     gad = CreateContext( glistptr );
  246.  
  247.     ng.ng_LeftEdge = 20;
  248.     ng.ng_TopEdge = sc->WBorTop + sc->Font->ta_YSize + 11;
  249.     ng.ng_Width = 120;
  250.     ng.ng_Height = 14;
  251.     ng.ng_Flags = 0;
  252.     ng.ng_VisualInfo = vi;
  253.     ng.ng_TextAttr = &topaz80;
  254.     ng.ng_GadgetText = "_Clear Pointer";
  255.     ng.ng_GadgetID = 'C';
  256.     gad = CreateGadget( BUTTON_KIND, gad, &ng,
  257.     GT_Underscore, '_',
  258.     TAG_DONE );
  259.  
  260.     ng.ng_LeftEdge += 130;
  261.     ng.ng_GadgetText = "_Simple Pointer";
  262.     ng.ng_GadgetID = 'S';
  263.     gad = CreateGadget( BUTTON_KIND, gad, &ng,
  264.     GT_Underscore, '_',
  265.     TAG_DONE );
  266.  
  267.     ng.ng_TopEdge += 20;
  268.     ng.ng_LeftEdge = 20;
  269.     ng.ng_GadgetText = "_Wide Pointer";
  270.     ng.ng_GadgetID = 'W';
  271.     gad = CreateGadget( BUTTON_KIND, gad, &ng,
  272.     GT_Underscore, '_',
  273.     TAG_DONE );
  274.  
  275.     ng.ng_TopEdge += 20;
  276.     ng.ng_LeftEdge = 20;
  277.     ng.ng_GadgetText = "_Busy Pointer";
  278.     ng.ng_GadgetID = 'B';
  279.     gad = CreateGadget( BUTTON_KIND, gad, &ng,
  280.     GT_Underscore, '_',
  281.     TAG_DONE );
  282.  
  283.     ng.ng_LeftEdge += 130;
  284.     ng.ng_GadgetText = "_Delayed Busy";
  285.     ng.ng_GadgetID = 'D';
  286.     gad = CreateGadget( BUTTON_KIND, gad, &ng,
  287.     GT_Underscore, '_',
  288.     TAG_DONE );
  289.  
  290.     return( gad );
  291. }
  292.  
  293. /*------------------------------------------------------------------------*/
  294.  
  295. void bail_out(long code)
  296. {
  297.     if (win)
  298.     CloseWindow(win);
  299.  
  300.     if ( glist )
  301.     FreeGadgets( glist );
  302.  
  303.     if ( vi )
  304.     FreeVisualInfo( vi );
  305.  
  306.     if ( mysc )
  307.         UnlockPubScreen( NULL, mysc );
  308.  
  309.     if (wide_pointer)
  310.     DisposeObject(wide_pointer);
  311.  
  312.     if (simple_pointer)
  313.     DisposeObject(simple_pointer);
  314.  
  315.     if (bm)
  316.     FreeBitMap(bm);
  317.  
  318.     if (GadToolsBase)
  319.     CloseLibrary(GadToolsBase);
  320.  
  321.     if (IntuitionBase)
  322.     CloseLibrary(IntuitionBase);
  323.  
  324.     if (GfxBase)
  325.     CloseLibrary(GfxBase);
  326.  
  327.     exit(code);
  328. }
  329.  
  330.  
  331. /*------------------------------------------------------------------------*/
  332.  
  333. #define PWORDWIDTH    4
  334. #define PHEIGHT        64
  335.  
  336. BOOL createPointers(void)
  337. {
  338.     struct RastPort rport;
  339.     int i;
  340.  
  341.     /* First, let's make a traditional bitmap, then make a
  342.      * pointer out of it:
  343.      */
  344.     InitBitMap( &simplebm, 2, 16, 16 );
  345.     simplebm.Planes[0] = (PLANEPTR)simplePointer0;
  346.     simplebm.Planes[1] = (PLANEPTR)simplePointer1;
  347.     simple_pointer = NewObject( NULL, "pointerclass",
  348.     POINTERA_BitMap, &simplebm,
  349.     POINTERA_XOffset, -6,
  350.     POINTERA_WordWidth, 1,
  351.     POINTERA_XResolution, POINTERXRESN_HIRES,
  352.     POINTERA_YResolution, POINTERYRESN_HIGH,
  353.     TAG_DONE );
  354.  
  355.     /* Now, we'll make a V39-style bitmap, and render into
  356.      * a RastPort we throw around it:
  357.      */
  358.  
  359.     if ( bm = AllocBitMap( 16*PWORDWIDTH, PHEIGHT, 2, BMF_CLEAR, NULL ) )
  360.     {
  361.     InitRastPort( &rport );
  362.     rport.BitMap = bm;
  363.  
  364.     SetAPen( &rport, 1 );
  365.     for ( i = 0; i < PHEIGHT; i++ )
  366.     {
  367.         Move( &rport, 0, i );
  368.         Draw( &rport, (i*(PWORDWIDTH*16))/PHEIGHT, i );
  369.     }
  370.  
  371.     SetAPen( &rport, 3 );
  372.     Move( &rport, 0 ,0 );
  373.     Draw( &rport, PWORDWIDTH*16-1, PHEIGHT-1 );
  374.     SetAPen( &rport, 2 );
  375.     Move( &rport, 0 ,0 );
  376.     Draw( &rport, 0, PHEIGHT-1 );
  377.  
  378.     wide_pointer = NewObject( NULL, "pointerclass",
  379.         POINTERA_BitMap, bm,
  380.         POINTERA_WordWidth, PWORDWIDTH,
  381.         POINTERA_XResolution, POINTERXRESN_HIRES,
  382.         POINTERA_YResolution, POINTERYRESN_HIGHASPECT,
  383.         TAG_DONE );
  384.     }
  385.  
  386.     return( (BOOL)(simple_pointer && wide_pointer) );
  387. }
  388.  
  389. /*------------------------------------------------------------------------*/
  390.  
  391.